﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain.Abstract;
using Domain.Concrete;
using Domain.Entity;
using Pro260Website.Models;

namespace Pro260Website.Controllers
{
    public class CompanyController : Controller
    {
        private ICompanyRepository repository;

        private const int DEFAULT_PAGE_SIZE = 10;

        public CompanyController(ICompanyRepository companyRepository)
        {
            repository = companyRepository;
        }

        public ViewResult Listings(string SearchField = "", int page = 1, int pageSize = DEFAULT_PAGE_SIZE)
        {
            var viewModel = GetViewModel(SearchField, page, pageSize);
            return View(viewModel);
        }

        [Authorize(Roles="Admin")]
        public ViewResult AdminListings(string SearchField = "", int page = 1, int pageSize = DEFAULT_PAGE_SIZE)
        {
            var viewModel = GetViewModel(SearchField, page, pageSize);
            return View(viewModel);
        }

        private ListingViewModel<Company> GetViewModel(string queryString, int page, int pageSize)
        {
            var items = repository.Companies;

            if (!string.IsNullOrWhiteSpace(queryString))
            {
                items = items = repository.SearchCompany(queryString).AsQueryable();
            }

            var viewModel = new ListingViewModel<Company>()
            {
                Items = items.OrderBy(c => c.Name).Skip((page - 1) * pageSize).Take(pageSize),
                PagingInfo = new PagingInfo
                {
                    CurrentPage = page,
                    ItemsPerPage = pageSize,
                    TotalItems = items.Count()
                },
                QueryInfo = new QueryInfo
                {
                    QueryString = queryString
                }
            };


            return viewModel;
        }

        //CRUD
        [Authorize(Roles="Admin")]
        public ViewResult CreateCompany()
        {
            return View("EditCompany", new Company());
        }

        [HttpPost]
        [Authorize(Roles="Admin")]
        public ActionResult EditCompany(Company company)
        {
            if (ModelState.IsValid)
            {
                repository.SaveCompany(company);
                TempData["message"] = string.Format("{0} has been saved", company.Name);
                return RedirectToAction("AdminListings");
            }
            else
            {
                return View(company);
            }
        }

        [Authorize(Roles="Admin")]
        public ActionResult EditCompany(int companyID)
        {
                Company company = repository.Companies.FirstOrDefault(c => c.CompanyID == companyID);
                return View(company);
        }

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public ActionResult DeleteCompany(int companyID)
        {
            Company company = repository.Companies.FirstOrDefault(c => c.CompanyID == companyID);
            if (company != null)
            {
                repository.RemoveCompany(company);
                TempData["message"] = string.Format("{0} was deleted", company.Name);
            }else{
                TempData["message"] = string.Format("That company isn't listed");
            }

            return RedirectToAction("AdminListings");
        }

        [Authorize(Roles = "Admin")]
        public ActionResult AddCompanyNews(int companyId)
        {
            var model = new CompanyNewsViewModel
            {
                CompanyId = companyId,
                Article = new CompanyNewsArticle
                {
                    PostedDate = DateTime.Now
                }
            };
            return View(model);
        }

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public ActionResult AddCompanyNews(CompanyNewsViewModel model)
        {
            if (ModelState.IsValid)
            {

                var company = repository.Companies.FirstOrDefault(p => p.CompanyID == model.CompanyId);
                company.Articles.Add(model.Article);
                repository.SaveCompany(company);
                return RedirectToAction("AdminListings");
            }
            return View(model);
        }
    }
}
